package com.puman.beautyfarmcounselor.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.andexert.library.RippleView;
import com.puman.beautyfarmcounselor.IndexActivity;
import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.http.AjaxCallBack;
import com.puman.beautyfarmcounselor.ui.core.BaseFragment;
import com.puman.beautyfarmcounselor.ui.home.model.Counselor;
import com.puman.beautyfarmcounselor.util.MethedUtil;
import com.puman.beautyfarmcounselor.util.config.GlobalData;
import com.puman.beautyfarmcounselor.util.config.RequestAddr;
import com.puman.beautyfarmcounselor.util.XTHttp;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.lang.ref.SoftReference;


/**
 * A placeholder fragment containing a simple view.
 */
@EFragment(R.layout.login_layout)
public class LoginFragment extends BaseFragment implements RippleView.OnRippleCompleteListener {

//    @ViewById(R.id.base_title)
//    TextView topTitle;

    @ViewById(R.id.login_account)
    EditText account;
    @ViewById(R.id.login_password)
    EditText password;
    @ViewById(R.id.login_in_out_btn)
    RippleView loginLayout;

    public LoginFragment() {
    }

    @AfterViews
    void updateView() {
        loginLayout.setOnRippleCompleteListener(this);
//        account.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                Log.i("curry", "actionId:" + actionId);
//                if (actionId == EditorInfo.IME_ACTION_GO) {
//                    /*隐藏软键盘*/
//                    InputMethodManager imm = (InputMethodManager) v
//                            .getContext().getSystemService(
//                                    Context.INPUT_METHOD_SERVICE);
//                    if (imm.isActive()) {
//                        imm.hideSoftInputFromWindow(
//                                v.getApplicationWindowToken(), 0);
//                    }
//                    password.requestFocus();
//                    return true;
//                }
//                return false;
//            }
//        });
    }

//    @Click({R.id.login_btn})
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.login_btn:
//                if (checkIsValidate()) {
//                    login();
//                }
//                break;
//        }
//        keyboardTakeBack(view);
//    }

    /**
     * 判断是否输入用户名和密码正确
     */
    private boolean checkIsValidate() {
        if (account.getText().toString().length() == 0 || password.getText().toString().length() == 0) {
            Toast.makeText(IndexActivity.getInstace(), "请填写用户名或密码", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            return true;
        }
    }

    void login() {
        XTHttp xtHttp = new XTHttp();
        xtHttp.get(RequestAddr.login + "password=" + password.getText() + "&staff_account=" + account.getText(), new AjaxCallBack<Object>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                if (saveData(o)){
                    Intent intent = new Intent();
                    intent.setClass(IndexActivity.getInstace(), MainActivity.class);
                    startActivity(intent);
                    IndexActivity.getInstace().finish();
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(IndexActivity.getInstace(), "网络不稳定，请稍后再试", Toast.LENGTH_SHORT).show();
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }


    private boolean saveData(Object object) {
        ObjectMapper objectMapper = new ObjectMapper();
//        counselor = objectMapper.readValue(object.toString(), Counselor.class);
//        MethedUtil.UtilLog("object"+object.toString());
        try {
            GlobalData.counselor = objectMapper.readValue(object.toString(), Counselor.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        MethedUtil.UtilToast(getActivity(), GlobalData.counselor.getMsg());
        if (GlobalData.counselor.isFlag()){
            GlobalData.staff_account = account.getText().toString();
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void onComplete(RippleView rippleView) {
        if (checkIsValidate()) {
            login();
        }

    }
}
